<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            width: 100%;
            height: 100%;
        }
        ul{
            display: none;
            
        }
    </style>
</head>
<body>
   <button>下拉菜单</button>
    <ul class="ul">
        <li>黄瓜</li>
        <li>菜瓜</li>
        <li>甜瓜</li>
        <li>西瓜</li>
    </ul>

    <script>
        var btn = document.querySelector('button');
        var ul = document.querySelector('.ul');
   
       
        var ul = document.querySelector('.ul')
        // 1. 阻止默认事件
        document.body.oncontextmenu = function (e) {
            // 4.跟随鼠标
            ul.style.top = e.clientY + 'px'
            ul.style.left = e.clientX + 'px'
            e.preventDefault();
            e.returnValue = false;
            // 2. 显示自定义菜单
            ul.style.display = 'block';
        }

        // 3. 点击空白隐藏菜单
        document.body.onclick = function(e) {
            e.stopPropagation(); // 普通浏览器
            event.cancelBubble=true; // ie浏览器
            ul.style.display = 'none';
        }
 

    
  </script>
</body>
</html>